var QUICK_TIMER = null;

(function(){
    /********initialize start *************/
    // 展示第一个panel
    $('#rule').css('display', 'flex');
    loadRules();
     /********initialize end *************/

    // menu bar.
    $('.header-tab-item').click(function (event) {
        $('.header-tab-item-active').removeClass('header-tab-item-active');
        $(event.target).addClass('header-tab-item-active');
        var targetName = $(event.target).attr('name');
        $('.content').hide();
        $('#' + targetName).css('display', 'flex');
    });

    
    // 规则

    /// 加载规则
    function loadRules() {
        var data = Store.loadRules();
        $('#ruleList').html('');
        var ele = []
        for (let index = 0; index < data.length; index++) {
            const element = data[index];
            var eleStr = `<div class='data-list-item' id=${element.id}>
                            <span>${element.ruleName}</span>
                            <span>${element.sharesName}(${element.sharesCode})</span>
                        </div>`;
            ele.push(eleStr);
        }
        $('#ruleList').html(ele.join(''));
    }
    function resetRuleForm() {
        let $ele = $('input[name="ruleBaseInfo"]');
        for(var ii=0; ii<$ele.length; ii++) {
            $($ele[ii]).val('');
        }
        // 清空类型
        $('input[name="ruleType"]:checked').attr('checked', false);
        $('#ruleBaseInfo_repeatNotifaction').attr('checked', false);

        for (var m=0;m<$('input[name="ruleValue"]').length;m++) {
            $($('input[name="ruleValue"]')[m]).val('');
        }
    }

    $('#saveRule').click(function() {
        let $ele = $('input[name="ruleBaseInfo"]');
        let obj = {};
        for(var i=0; i<$ele.length; i++) {
            let id = $($ele[i]).attr('id');
            obj[id.split('_')[1]] = $($ele[i]).val();
        }
        // ruletype
        let $eleRadio = $('input[name="ruleType"]:checked');
        if ($eleRadio && $eleRadio.length === 1) {
            let type = $($eleRadio[0]).attr('id');
            let val = $($('input[name="ruleType"]:checked')[0]).parent().next().next().children().val();
            if (val == null && val  === '') {
                alert('请填写规则阈值！');
                return;
            }
            obj.ruleType = type;
            obj.ruleValue = val;
        } else {
            alert('请至少选择一个规则！');
            return;
        }
        // 重复通知
        obj['repeact'] = $('#ruleBaseInfo_repeatNotifaction').attr('checked');
        Store.updateRules(obj);
        loadRules();
        resetRuleForm();
    });

    $('.data-list-item').click(function(event) {
        $('.data-list-item').removeClass('data-list-item-active');
        $(event.target).addClass('data-list-item-active');
        $('.operation-area').show(50);
    });

    $('#ruleBaseInfo_sharesName').focus(function (e) { 
        $('#sharesSearchSelect').show(50);
        $('#sharesSearchSelect').html('');
        e.preventDefault(); 
    });
    $('#ruleBaseInfo_sharesName').bind("input propertychange", function(){
        var val = $('#ruleBaseInfo_sharesName').val();
        if (QUICK_TIMER != null) {
            clearTimeout(QUICK_TIMER);
        }
        QUICK_TIMER = setTimeout(() => {
            $.get(`http://suggest3.sinajs.cn/suggest/type=&key=${val}`, (res) => {
                let options = [];
                if (res === 'var suggestvalue="";') {
                    var option = `<span class='search-select-option' disabled>无匹配数据</span>`;
                    options.push(option);
                } else {
                    res = res.replace('var suggestvalue="', '').replace('";', '');
                    var arr = res.split(';');
                    let flag = 0;
                    
                    for (var i=0; i<arr.length; i++) {
                        let detail = arr[i].split(',');
                        var option = `<span class='search-select-option' id='${arr[i]}'>${detail[4]}</span>`;
                        options.push(option);
                        flag ++ ;
                        if (flag > 10) {
                            break;
                        }
                    }
                }
                $('#sharesSearchSelect').html(options.join(''));
            });
        }, 200);
    });

    $('#ruleBaseInfo_sharesName').blur(function(e) {
        $('#sharesSearchSelect').hide(50);
        $('#ruleBaseInfo_sharesName').val('');
        e.preventDefault();
    });
    $('#sharesSearchSelect').click(function(e) {
        if ($(e.target).attr('class') === 'search-select-option') {
            var selectVal = $(event.target).attr('id');
            let detail = selectVal.split(',');
            $('#ruleBaseInfo_sharesName').val(detail[4]);
            $('#ruleBaseInfo_sharesCode').val(detail[0]);
            e.preventDefault();
        }
    });

    $(document).bind("click", function(event){
        if (! event.target === $('#ruleBaseInfo_sharesName').target) {
            $("#sharesSearchSelect").hide(50);
        }
    })
        
        

    // 设置

    // 支持

    // 关于我
    
})()